import { Component, Injector, OnInit } from '@angular/core';
import { SFUISchema } from '@delon/form';
import { STComponentBase } from '@shared/itcore/components/st-component-base';
import { AjaxResult } from '@shared/itcore/itcore.model';
import { ItcoreSTColumn } from '@shared/itcore/services/alain.types';

@Component({
  selector: 'app-role-entityinfo',
  templateUrl: './role-entityinfo.component.html',
  styles: [],
})
export class RoleEntityinfoComponent extends STComponentBase implements OnInit {
  constructor(injector: Injector) {
    super(injector);
    this.moduleName = 'roleEntity';
  }

  ngOnInit() {
    super.InitBase();
  }

  protected GetSTColumns(): ItcoreSTColumn[] {
    const columns: ItcoreSTColumn[] = [
      {
        title: '操作',
        fixed: 'left',
        width: 65,
        buttons: [
          {
            text: '操作',
            children: [
              {
                text: '编辑',
                icon: 'edit',
                acl: 'Root.Admin.Auth.RoleEntity.Update',
                iif: (row) => row.Updatable,
                click: (row) => this.edit(row),
              },
              {
                text: '删除',
                icon: 'delete',
                type: 'del',
                acl: 'Root.Admin.Auth.RoleEntity.Delete',
                iif: (row) => row.Deletable,
                click: (row) => this.delete(row),
              },
            ],
          },
        ],
      },
      {
        title: '角色',
        index: 'RoleId',
        type: 'number',
        className: 'text-left',
        format: (d) => `${d.RoleId}. ${d.RoleName}`,
        editable: true,
      },
      { title: '数据实体', index: 'EntityId', className: 'text-left', format: (d) => `${d.EntityName} [${d.EntityType}]`, editable: true },
      {
        title: '操作',
        index: 'Operation',
        type: 'tag',
        tag: this.alain.DataAuthOperationTags,
        className: 'text-center',
        editable: true,
        fenum: this.toEnum(this.itcore.data.dataAuthOperations),
      },
      { title: '锁定', index: 'IsLocked', type: 'yn', editable: true },
      { title: '注册时间', index: 'CreatedTime', type: 'date' },
    ];
    return columns;
  }

  protected GetSFUISchema(): SFUISchema {
    const ui: SFUISchema = {
      '*': { spanLabelFixed: 100, grid: { span: 12 } },
      $RoleId: {
        grid: { span: 24 },
        widget: 'select',
        asyncData: () => this.alain.ReadNode('api/admin/role/readNode', 'RoleName', 'RoleId'),
      },
      $EntityId: {
        grid: { span: 24 },
        widget: 'select',
        asyncData: () => this.alain.ReadNode('api/admin/entityinfo/readNode', 'Name', 'Id'),
      },
    };
    return ui;
  }

  showGroupJson(item: any) {
    item.groupJson = JSON.stringify(item.FilterGroup, null, 2);
  }
  saveFilterGroup(item: any) {
    const dto = {
      Id: item.Id,
      RoleId: item.RoleId,
      EntityId: item.EntityId,
      Operation: item.Operation,
      FilterGroup: item.FilterGroup,
      IsLocked: item.IsLocked,
    };
    this.http
      .post<AjaxResult>('api/admin/roleEntity/update', [dto])
      .subscribe((result) => {
        this.itcore.ajaxResult(result);
      });
  }
}
